package com.songlanyun.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
import com.songlanyun.common.drive.IntegerEnum;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.custom.DateCustomUtils;

import java.util.Date;
import java.util.Objects;

/**
 * 活动 相关
 */
public class MarketingConstant {


    /**
     * 周期方式
     * 0：每天
     * 1：每周
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public static enum CycleMode implements IntegerEnum<Integer> {

        EVERYDAY(0, "每天"),

        EVERY_WEEK(1, "每周"),

        ;

        @EnumValue
        @JsonValue
        private Integer code;

        private String msg;

        CycleMode(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @JsonCreator
        public static MarketingConstant.CycleMode getByCode(int code) {
            for (MarketingConstant.CycleMode value : MarketingConstant.CycleMode.values()) {
                if (Objects.equals(code, value.getCode())) {
                    return value;
                }
            }
            return null;
        }
    }

    /**
     * 周几
     * 0：周日
     * 1：周一
     * 2：周二
     * 3：周三
     * 4：周四
     * 5：周五
     * 6：周六
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public static enum Week implements IntegerEnum<Integer> {

        SUNDAY(0, "周日"),

        MONDAY(1, "周一"),

        TUESDAY(2, "周二"),

        WEDNESDAY(3, "周三"),

        THURSDAY(4, "周四"),

        FRIDAY(5, "周五"),

        SATURDAY(6, "周六"),

        ;

        @EnumValue
        @JsonValue
        private Integer code;

        private String msg;

        Week(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @JsonCreator
        public static MarketingConstant.Week getByCode(int code) {
            for (MarketingConstant.Week value : MarketingConstant.Week.values()) {
                if (Objects.equals(code, value.getCode())) {
                    return value;
                }
            }
            return null;
        }
    }

    /**
     * 周期方式
     * 0：固定折扣
     * 1：固定减价
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public static enum PriceType implements IntegerEnum<Integer> {

        FIXED_DISCOUNT(0, "固定折扣"),

        PRICE_FIXED_BY(1, "固定减价"),

        ;

        @EnumValue
        @JsonValue
        private Integer code;

        private String msg;

        PriceType(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @JsonCreator
        public static MarketingConstant.PriceType getByCode(int code) {
            for (MarketingConstant.PriceType value : MarketingConstant.PriceType.values()) {
                if (Objects.equals(code, value.getCode())) {
                    return value;
                }
            }
            return null;
        }
    }


    /**
     * 活动状态
     * 0：未开始
     * 1：进行中
     * 2：已结束
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public static enum ActiveState implements IntegerEnum<Integer> {

        NOT_START(0, "未开始"),
        UNDERWAY(1, "进行中"),
        FINISHED(2, "已结束"),
        ;

        @EnumValue
        @JsonValue
        private Integer code;

        private String msg;

        ActiveState(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @JsonCreator
        public static ActiveState getByCode(int code) {
            for (ActiveState value : ActiveState.values()) {
                if (Objects.equals(code, value.getCode())) {
                    return value;
                }
            }
            return null;
        }
    }


    /**
     * 活动类型
     * 0：无活动
     * 1：秒杀
     * 2：拼团
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public static enum ActivityType implements IntegerEnum<Integer> {

        NOT(0, "无活动"),

        SECKILL(1, "秒杀"),

        GROUP_BUY(2, "拼团"),

        ;

        @EnumValue
        @JsonValue
        private Integer code;

        private String msg;

        ActivityType(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @JsonCreator
        public static ActivityType getByCode(int code) {
            for (ActivityType value : ActivityType.values()) {
                if (Objects.equals(code, value.getCode())) {
                    return value;
                }
            }
            return null;
        }
    }




    /**
     * 获得秒杀活动状态
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static ActiveState getSeckillActiveState(Date startTime, Date endTime, String startHour, String endHour) {
        Date date = new Date();
        ActiveState activeState;//活动状态

        try {
            if (startTime.getTime() > date.getTime()) {
                //未开始
                activeState = MarketingConstant.ActiveState.NOT_START;
            } else if (DateUtils.addDateDays(endTime, 1).getTime() < date.getTime()) {
                //已结束
                activeState = MarketingConstant.ActiveState.FINISHED;
            } else {
                //当天有活动
                //开始时间
                Date startDate = DateUtils.stringToDate(DateCustomUtils.dateToString(date) + " " + startHour);
                //结束时间
                Date endDate = DateUtils.stringToDate(DateCustomUtils.dateToString(date) + " " + endHour);
                if (date.getTime() < startDate.getTime()) {
                    //未开始
                    activeState = MarketingConstant.ActiveState.NOT_START;
                } else if (date.getTime() <= endDate.getTime()) {
                    //进行中
                    activeState = MarketingConstant.ActiveState.UNDERWAY;
                } else {
                    //已结束
                    activeState = MarketingConstant.ActiveState.FINISHED;
                }
            }
        } catch (Exception e) {
            activeState = MarketingConstant.ActiveState.FINISHED;
        }

        return activeState;
    }

    /**
     * 获得秒杀活动状态
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static ActiveState getSeckillActiveTimeState(Date startTime, Date endTime, String startHour, String endHour) {
        Date date = new Date();
        ActiveState activeState;//活动状态

        //开始时间
        Date startDate = DateUtils.stringToDate(DateCustomUtils.dateToString(startTime) + " " + startHour);
        //结束时间
        Date endDate = DateUtils.stringToDate(DateCustomUtils.dateToString(endTime) + " " + endHour);
        if (date.getTime() < startDate.getTime()) {
            //未开始
            activeState = MarketingConstant.ActiveState.NOT_START;
        } else if (date.getTime() <= endDate.getTime()) {
            //进行中
            activeState = MarketingConstant.ActiveState.UNDERWAY;
        } else {
            //已结束
            activeState = MarketingConstant.ActiveState.FINISHED;
        }

        return activeState;
    }


    /**
     * 获得拼团活动状态
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static ActiveState getGroupBuyActiveState(Date startTime, Date endTime) {
        long timeMillis = System.currentTimeMillis();

        ActiveState activeState;//活动状态
        //开始时间
        Date startDate = startTime;
        //结束时间  结束日期加1天
        Date endDate = DateUtils.addDateDays(endTime, 1);

        if (timeMillis < startDate.getTime()) {
            //未开始
            activeState = ActiveState.NOT_START;
        } else if (timeMillis >= endDate.getTime()) {
            //已结束
            activeState = ActiveState.FINISHED;
        } else {
            //已进行中
            activeState = ActiveState.UNDERWAY;
        }

        return activeState;
    }


}
